package com.bunker.client;

import java.util.ArrayList;


import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.core.client.GWT;


public class Entity {
	private String id;
	
	private Point position, hotspot;
	private float scale, rotation;
	private int width, height;
	
	private ArrayList<Rendercomponent> renderComponents = new ArrayList<Rendercomponent>();
	private ArrayList<Component> components = null;
	
	private boolean toBeRemove;
	
	// constructor
	public Entity(String id) {
		this.id = id;
		
		components = new ArrayList<Component>();
		
		position = new Point();
		hotspot = new Point();
		
		scale = 1;
		rotation = 0;
		
		width = 0;
		height = 0;
		
		toBeRemove = false;
	}
	
	// add Component
	public void AddComponent(Component comp) {
		
		comp.setOwnerEntity(this);
		
		// if comp is a component
		components.add(comp);
	}
	
	// add renderComponent
	public void AddRendercomponent(Rendercomponent rcom) {

		// if comp is a render component
		rcom.setOwnerEntity(this);
		renderComponents.add(rcom);
	}
	
	/*
	 *  GETTER
	 */
	public Component getComponent(String id) {
		
		for(Component c : components) 
			if(c.getId().equals(id))
				return c;
				
		return null;
	}
	
	public ArrayList<Component> getComponentList() {
		return components;
	}
	
	public ArrayList<Rendercomponent> getRenderComponentList() {
		return renderComponents;
	}
	
	public Point getPosition() {
		return position;
	}
	
	public float getScale() {
		return scale;
	}
	
	public float getRotation() {
		return rotation;
	}
	
	public String getId() {
		return id;
	}
	
	public int getWidth() {
		return width;
	}
	
	public int getHeight() {
		return height;
	}
	
	public Point getHotspot() {
		return hotspot;
	}
	
	public boolean isToRemove() {
		return toBeRemove;
	}
	
	/*
	 *  SETTER
	 */
	public void setPosition(Point p) {
		position = p;
	}
	
	public void setRotation(float rotate) {
		rotation = rotate;
	}
	
	public void setScale(float s) {
		scale = s;
	}
	
	public void setWidth(int w) {
		width = w;
	}
	
	public void setHeight(int h) {
		height = h;
	}
	
	public void setHotspot(Point hs) {
		hotspot = hs;
	}
	
	public void toRemove() {
		toBeRemove = true;
	}
	
	/*
	 * UPDATE AND RENDER
	 */
	public void update(GameContainer gc) {
		for(Component c : components)
			c.update(gc);
	}
	
	public void render(GameContainer gc, Canvas c) {
		if(renderComponents != null) {
			for(Rendercomponent r : renderComponents) {
				r.render(gc, c);
			}
		}
	}
	
	/*
	 * Collision detector
	 */
	
	public boolean colusionTest(Entity e) {
		/*
		thisEntity = new Collision(position.x, position.y, width, height);
		
		GWT.log(id + "'s position: " + "(" + position.x + "," + position.y + ")");
		GWT.log(id + "'s size: " + "(" + width + "," + height + ")");
		
		thatEntity = new Collision(e.position.x, e.position.y, e.getWidth(), e.getHeight());
		//GWT.log(e.id + "'s position: " + "(" + thatEntity.x + "," + thatEntity.y + ")");
		//GWT.log(e.id + "'s size: " + "(" + e.width + "," + e.height + ")");
		*/
		
		return colideOn(this, e);
	}
	
	private boolean colideOn(Entity thisEntity, Entity thatEntity) {
		
		boolean xOverlap = valueInRange(thisEntity.getPosition().x, thatEntity.getPosition().x, thatEntity.getPosition().x + thatEntity.getWidth()) ||
                    	   valueInRange(thatEntity.getPosition().x, thisEntity.getPosition().x, thisEntity.getPosition().x + thisEntity.getWidth());
		
		boolean yOverlap = valueInRange(thatEntity.getPosition().y, thatEntity.getPosition().y, thatEntity.getPosition().y + thatEntity.getHeight()) ||
                    	   valueInRange(thatEntity.getPosition().y, thatEntity.getPosition().y, thatEntity.getPosition().y + thatEntity.getHeight());
		
		return xOverlap && yOverlap;

	}
	
	private boolean valueInRange(int value, int min, int max) {
		return (value >= min) && (value <= max);
	}
	
}